package Classes.GameViews.ViewIcons// ActionScript file
{
	import Classes.GameDatas.GD_BuildingItem;
	import Classes.GameDatas.GD_Mission;
	import Classes.GameDatas.GameData;
	import Classes.GameUtils.GU_GetBMPMC;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameViews.GameView;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 *图标显示类 
	 * @author 葛增文
	 * 
	 */	
	public class ViewIcon extends GameView implements I_NeedSWFSource
	{
		private var _sourceName:String;
		private var _picName:String;
		private var _numSP:Sprite;
		private var _stateSP:Sprite;
		private var _stepSp:Sprite;
		private var _dataTarget:GameData;
		private var _displayViewIconName:String;
		private var _type:uint = 0;		//1 为显示x0,和x1 2为显示∞无限
		
		private var _iWidth:int;
		private var _iHeight:int;
		
		public function ViewIcon(sourceName:String,picName:String,w:int=0,h:int=0):void
		{
			this._iWidth=w;
			this._iHeight=h;
			this._sourceName=sourceName;
			this._picName=picName;
			this.initFun();
			this.type=1;
		}
		
		public function initFun():void
		{
			M_LoadSWFSource.instance.needLoad(this,this.sourcePath,this.sourceName)
			this._numSP=new (ApplicationDomain.currentDomain.getDefinition("FrameNumTxt_v") as Class);
			this.numSP.visible=false;
			this.addChild(this.numSP);
			this._stateSP=new Sprite;
			this.stateSP.x=60;
			this.stateSP.y=0;
			this.addChild(this.stateSP);
			
			this._stepSp=new Sprite();
			this.stepSp.x=14;
			this.stepSp.y=42;
			this.addChild(stepSp);
			this.stepSp.visible=false;
		}
		
		/*private function addObserverFunc():void
		{
		this.dataTarget.addObserver("itemNumber",this);
		}
		
		public function touchObserve(observeType:String):void
		{
		switch(observeType)
		{
		case "itemNumber":
		this.showNum();
		break;
		}
		}
		
		private function showNum():void
		{
		this.setNum(this.dataTarget.itemNumber);
		}*/
		
		public function setNum(value:int):void
		{
			
			if(value==0 || value==1 )
			{
				this.numSP.visible=false;
				this.numSP["num"]=value;
			}
			else
			{
				this.numSP.visible=true;
				this.numSP["num"]=value;
			}
			
			upDataType();
			
		}
		
		public function setNumSpVisiable(isOpenNumSprite:Boolean=true):void
		{
			this.numSP.visible=isOpenNumSprite;
		}
		
		/**
		 * 设置滤镜   
		 * @param iconFilter  参数为空  默认为灰色滤镜
		 */		
		public function setFilter(iconFilter:Array=null):void
		{
			var matrix:Array = new Array();
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // red
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // green
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // blue
			
			matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
			
			var gray:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			
			var filtersArray:Array = new Array(gray);
			if(null==iconFilter)
			{
				iconFilter=filtersArray;
			}
			this.filters = iconFilter;
		}
		
		public function resetNum():void
		{
			this.numSP["reset"]();
		}
		
		public function getNum():Number
		{
			return this.numSP["num"];
		}
		private var timer:Timer;
		public function setState(value:String):void
		{
			while(this.stateSP.numChildren>0)this.stateSP.removeChildAt(0);
			if(value)
			{
				if(value=="")return;
				if(null!=timer)
				{
					timer.stop();
					timer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
					timer=null;
				}
				var statePic:DisplayObject=new (ApplicationDomain.currentDomain.getDefinition("iconState_"+value) as Class);
				statePic.width=60;
				statePic.height=45;
				this.stateSP.addChild(statePic);
				if(value!="doing")
				{
					var arrow:DisplayObject=this.stateSP.addChild(GU_dealPower.instance.getLeftArrow());
					arrow.x=60;
					arrow.y=42;
					arrow.scaleX=arrow.scaleX*1.3;
					arrow.scaleY=arrow.scaleY*1.3;
				}
				if(value=="doing")
				{
					timer=new Timer(1000,8);
					timer.addEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
					timer.start();
				}
			}
		}
		
		public function  setStep(value:Number):void
		{
			if(value<=0 || isNaN(value))return;
//			var lmsz:String=this.turnNumberToLuoma(value);
			//暂时用文本 以后可能用元件
			var tf:TextField=new TextField();
			tf.mouseEnabled=false;
			tf.selectable=false;
			tf.htmlText="<font color='#33FFFF' size='40px' face='Showcard Gothic'>"+value+"</font>";
			tf.filters=[new GlowFilter(0x000000,1,3,3,4,BitmapFilterQuality.LOW)];
			this.stepSp.visible=true;
			this.stepSp.addChild(tf);
		}
		
		
		private function turnNumberToLuoma(value:Number):String
		{
			var tmpStr:String="";
			if(value>=10){
				tmpStr="X";
			}else if(value>=20){
				tmpStr="XX";
			}
			var ms:Number=value%10;
			switch(ms)
			{
				case 1:
					tmpStr=tmpStr+"IVx";
					break;
				case 2:
					tmpStr=tmpStr+"II";
					break;
				case 3:
					tmpStr=tmpStr+"III";
					break;
				case 4:
					tmpStr=tmpStr+"IV";
					break;
				case 5:
					tmpStr=tmpStr+"V";
					break;
				case 6:
					tmpStr=tmpStr+"VI";
					break;
				case 7:
					tmpStr=tmpStr+"VII";
					break;
				case 8:
					tmpStr=tmpStr+"VIII";
					break;
				case 9:
					tmpStr=tmpStr+"IX";
					break;
			}
			return tmpStr;
		}
		
		
		private function completeTimerHalder(evt:TimerEvent):void
		{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
			timer=null;
			while(this.stateSP.numChildren>0)this.stateSP.removeChildAt(0);
		}
		
		/**
		 *创建ICON显示对象 
		 * @param iconType
		 * 
		 */		
		public static function creat(creatObj:Object):ViewIcon
		{
			var returnVIC:ViewIcon;
			if(creatObj.creatType)
			{
				switch(creatObj.creatType)
				{
					case "item":
						returnVIC=new VI_Item(creatObj.dataTarget);returnVIC.type=0;
						break;
					case "mission":
						returnVIC=new VI_Mission(creatObj.dataTarget as GD_Mission);returnVIC.type=0;
						break;
					case "buildingItem":
						returnVIC=new VI_BuildingItem(creatObj.dataTarget as GD_BuildingItem);returnVIC.type=0;
						break;
				}
				
			}
			else
			{
				returnVIC=new ViewIcon(creatObj.sourceName,creatObj.picName);returnVIC.type=0;
			}
			if(creatObj.creatNum)
			{
				returnVIC.setNum(creatObj.creatNum);returnVIC.type=0;
			}
			if(creatObj.creatState)
			{
				returnVIC.setState(creatObj.creatState);returnVIC.type=0;
			}
			
			return returnVIC;
		}
		
		public function sourceLoadOver(sourceName:String):void
		{
			var picDisplayer:DisplayObject=GU_GetBMPMC.getVectorSP(this.picName);
			var dobj:DisplayObject=this.addChildAt(picDisplayer,0);
			if(this.iHeight>0)
			{
				this.height=this.iHeight;
				this.iHeight=0;
			}
			if(this.iWidth>0)
			{
				this.width=this.iWidth;
				this.iWidth=0;
			}
			this.dispatchEvent(new Event("sourceLoadOver",true));
		}
		
		public function release():void
		{
			if(this.parent)this.parent.removeChild(this);
		}
		
		/*getter & setter*/
		public function get picName():String
		{
			return _picName;
		}
		
		public function get sourceName():String
		{
			return _sourceName;
		}
		
		public function get numSP():Sprite
		{
			return _numSP;
		}
		
		public function get stateSP():Sprite
		{
			return _stateSP;
		}
		
		public function get sourcePath():String
		{
			return M_ConfigData.instance.imgUrl+this.sourceName;
		}
		
		public  function resetNumspLocation(_x:int,_y:int):void
		{
			this.numSP.x=_x;
			this.numSP.y=_y;
		}
		
		public function get displayViewIconName():String
		{
			return _displayViewIconName;
		}
		
		public function set displayViewIconName(value:String):void
		{
			_displayViewIconName = value;
		}
		
		public function get stepSp():Sprite
		{
			return _stepSp;
		}
		
		public function set type(value:uint):void
		{
			this._type	= value;
			upDataType();
		}
		
		public function get type():uint
		{
			return this._type;
		}
		
		private var infinitySp:Sprite;
		private function upDataType():void
		{
			if(this.numSP)
				switch(this._type){
					case 0:
						if(this.numSP["num"]==0 || this.numSP["num"]==1)
							this.numSP.visible	=	false;
						else
							this.numSP.visible	=	true;
						break;
					case 1:this.numSP.visible=true;break;
					case 2:
						if(this._type==2)
						{
							this.numSP.visible=false;
							if(null==infinitySp)infinitySp=new (ApplicationDomain.currentDomain.getDefinition("infinity_v") as Class);
							//					this.numSP["num"]="99∞";
							this.addChild(infinitySp);
							infinitySp.scaleX	=2;
							infinitySp.scaleY	=2;
							infinitySp.x	=	30;
							infinitySp.y	=	80;
							
						}
						//∞
						break;
						
				}
				//∞
		}

		public function get iWidth():int
		{
			return _iWidth;
		}

		public function set iWidth(value:int):void
		{
			_iWidth = value;
		}

		public function get iHeight():int
		{
			return _iHeight;
		}

		public function set iHeight(value:int):void
		{
			_iHeight = value;
		}

		
	}
}